package com.citcc.web.controller.business;


import com.citcc.business.bean.CdzStake;
import com.citcc.business.service.SysViewService;
import com.citcc.common.utils.IpUtils;
import com.citcc.common.utils.StringUtils;
import com.citcc.framework.web.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.rmi.log.LogInputStream;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/business/sysView")
public class SysViewController extends BaseController {

    @Autowired
    private SysViewService sysViewService;

    @RequestMapping("/getCountAll")
    @ResponseBody
    public Object getCountAll(HttpServletRequest request){
        Map<String,Map<String,Object>> list = new HashMap<>();
        String orgId=getSysUser().getDeptId();
        list.put("stake",sysViewService.getCountStake(orgId));
        list.put("station",sysViewService.getCountStation(orgId,sysViewService.getOnLineStakeList(getUrl(request))));
        list.put("sumCharge",sysViewService.getSumCharge(orgId));
        list.put("sumPayNumber",sysViewService.getSumPayNumber(orgId));
        List<String> count = sysViewService.getOnLineStakeList(getUrl(request));
        Map<String,Object> map = new HashMap<>();
        if(count.size() == 1 && "isnull".equals(count.get(0))){
            map.put("STAKE_COUNT_ONLINE",0);
        }else{
            List<CdzStake> stakeList = sysViewService.getStakeOnline(orgId,count);
            map.put("STAKE_COUNT_ONLINE",stakeList.size());
        }
        list.put("online",map);
        return list;
    }
    @RequestMapping("/getCountStake")
    @ResponseBody
    public Object getCountStake(){
        Map<String,Object> map = sysViewService.getCountStake(getSysUser().getDeptId());
        return map;
    }
    @RequestMapping("/getCountStation")
    @ResponseBody
    public Object getCountStation(HttpServletRequest request){
        Map<String,Object> map = sysViewService.getCountStation(getSysUser().getDeptId(),sysViewService.getOnLineStakeList(getUrl(request)));
        return map;
    }
    @RequestMapping("/getSumCharge")
    @ResponseBody
    public Object getSumCharge(){
        Map<String,Object> map = sysViewService.getSumCharge(null);
        return map;
    } @RequestMapping("/getSumPayNumber")
    @ResponseBody
    public Object getSumPayNumber(){
        Map<String,Object> map = sysViewService.getSumPayNumber(null);
        return map;
    }
    @RequestMapping("/getRealState")
    @ResponseBody
    public Object getRealState(String type,HttpServletRequest request){
        Map<String,Object> map;
        String orgId= getSysUser().getDeptId();
        String url = getUrl(request);
        if("0".equals(type)){
            map= sysViewService.getRealState(type,orgId,"");
            map.putAll(getOnLine(request));
        }else{
            map = sysViewService.getRealState(type,orgId,sysViewService.getOnLineStakeList(url));
        }
        return map;
    }
    @RequestMapping("/getSumAll")
    @ResponseBody
    public Object getSumAll(String day){
        String orgId =getSysUser().getDeptId();
        List<Map<String,Object>> list = sysViewService.getSumAll(day,orgId);
        return list;
    }
    public Map<String,Object> getOnLine(HttpServletRequest request){
        String url = getUrl(request);
        return sysViewService.getOnLine(url);
    }
    public String getUrl(HttpServletRequest request){
        String url = IpUtils.getIpAddr(request);
        Boolean result = false;
        if(url.contains("127.0.0.1") || url.contains("locallhost") || (url.contains("111.7.172.183"))){
            if(url.contains("111.7.172.183")){
                int post = request.getRemotePort();
                if(post != 23981){
                    result = true;
                }
            }
        }else{
            result = true;
        }
        if(result){
            url = "http://111.7.172.183:22980";
        }else{
            url = "http://111.7.172.183:23980";
        }
        return url;
    }
}
